#include "editor.hpp"

void GuiEditor::Init() {
    engine.SetClearColor(color(100, 100, 100));
    engine.SetFPS(120);

    loadImages();

    auto toolboxLayout = gui::CreateLinearLayout(TOOLBOX_LAYOUT, false);
    toolboxLayout->SetPosition(point(0, 0));

    auto toolboxWindowBtn = gui::CreateImageButton(TOOLBOX_WINDOW,
                                                   storage_.Get("window_icon"));
    toolboxWindowBtn->SetClickCb([&](gui::Widget*){
                                    curNode_->Append(std::string("Widget") + std::to_string(++widgetNum_),
                                                     storage_.Get("window_icon"));
                                    treelistLayout_->Pack();
                                });
    toolboxLayout->AddWidget(toolboxWindowBtn);

    auto toolboxFoldWindowBtn = gui::CreateImageButton(TOOLBOX_FOLDWINDOW,
                                                       storage_.Get("foldwindow_icon"));
    toolboxFoldWindowBtn->SetClickCb([&](gui::Widget*){
                                        curNode_->Append(std::string("Widget") + std::to_string(++widgetNum_),
                                                         storage_.Get("foldwindow_icon"));
                                        treelistLayout_->Pack();
                                    });
    toolboxLayout->AddWidget(toolboxFoldWindowBtn);

    auto toolboxButtonBtn = gui::CreateImageButton(TOOLBOX_BUTTON,
                                                   storage_.Get("button_icon"));
    toolboxButtonBtn->SetClickCb([&](gui::Widget*){
                                     curNode_->Append(std::string("Widget") + std::to_string(++widgetNum_),
                                                      storage_.Get("button_icon"));
                                     treelistLayout_->Pack();
                                 });
    toolboxLayout->AddWidget(toolboxButtonBtn);

    auto toolboxImageButtonBtn = gui::CreateImageButton(TOOLBOX_IMAGEBUTTON,
                                                        storage_.Get("imagebutton_icon"));
    toolboxImageButtonBtn->SetClickCb([&](gui::Widget*){
                                         curNode_->Append(std::string("Widget") + std::to_string(++widgetNum_),
                                                          storage_.Get("imagebutton_icon"));
                                         treelistLayout_->Pack();
                                      });
    toolboxLayout->AddWidget(toolboxImageButtonBtn);

    auto toolboxCheckboxBtn = gui::CreateImageButton(TOOLBOX_CHECKBOX,
                                                     storage_.Get("checkbox_icon"));
    toolboxCheckboxBtn->SetClickCb([&](gui::Widget*){
                                         curNode_->Append(std::string("Widget") + std::to_string(++widgetNum_),
                                                          storage_.Get("checkbox_icon"));
                                         treelistLayout_->Pack();
                                   });
    toolboxLayout->AddWidget(toolboxCheckboxBtn);

    auto toolboxLabelBtn = gui::CreateImageButton(TOOLBOX_LABEL,
                                                  storage_.Get("label_icon"));
    toolboxLabelBtn->SetClickCb([&](gui::Widget*){
                                    curNode_->Append(std::string("Widget") + std::to_string(++widgetNum_),
                                                     storage_.Get("label_icon"));
                                    treelistLayout_->Pack();
                                 });
    toolboxLayout->AddWidget(toolboxLabelBtn);

    auto toolboxSlidebarBtn = gui::CreateImageButton(TOOLBOX_SLIDEBAR,
                                                     storage_.Get("slidebar_icon"));
    toolboxSlidebarBtn->SetClickCb([&](gui::Widget*){
                                       curNode_->Append(std::string("Widget") + std::to_string(++widgetNum_),
                                                        storage_.Get("slidebar_icon"));
                                       treelistLayout_->Pack();
                                   });
    toolboxLayout->AddWidget(toolboxSlidebarBtn);

    auto toolboxInputboxBtn = gui::CreateImageButton(TOOLBOX_INPUTBOX,
                                                     storage_.Get("inputbox_icon"));
    toolboxInputboxBtn->SetClickCb([&](gui::Widget*){
                                       curNode_->Append(std::string("Widget") + std::to_string(++widgetNum_),
                                                        storage_.Get("inputbox_icon"));
                                       treelistLayout_->Pack();
                                   });
    toolboxLayout->AddWidget(toolboxInputboxBtn);

    auto toolboxTreelistBtn = gui::CreateImageButton(TOOLBOX_TREELIST,
                                                     storage_.Get("treelist_icon"));
    toolboxTreelistBtn->SetClickCb([&](gui::Widget*){
                                       curNode_->Append(std::string("Widget") + std::to_string(++widgetNum_),
                                                        storage_.Get("treelist_icon"));
                                       treelistLayout_->Pack();
                                   });
    toolboxLayout->AddWidget(toolboxTreelistBtn);

    auto treelistWindow = gui::CreateWindow(TREELIST_WINDOW,
                                            point(0, toolboxLayout->GetSize().h),
                                            size(300, engine.GetCanvaSize().h - toolboxLayout->GetSize().h),
                                            "场景树",
                                            false);

    treelistLayout_ = gui::CreateLinearLayout(gui::ID_ANY);
    treelistWindow->SetLayout(treelistLayout_);

    treelist_ = gui::CreateTreeList(TREELIST,
                                    "场景根节点",
                                    storage_.Get("treelist_root_icon"));
    treelist_->Pack();

    treelist_->SetClickNodeCb([&](gui::TreeList::Node& node) { curNode_ = &node; });
    curNode_ = &treelist_->GetRoot();

    treelistLayout_->AddWidget(treelist_);
}

void GuiEditor::Render(Renderer&) {
    gui::Render();
}

void GuiEditor::Shutdown() {

}

void GuiEditor::loadImages() {
    storage_.Load("./assets/window_icon.bmp", "window_icon");
    storage_.Load("./assets/button_icon.bmp", "button_icon");
    storage_.Load("./assets/imagebutton_icon.bmp", "imagebutton_icon");
    storage_.Load("./assets/foldwindow_icon.bmp", "foldwindow_icon");
    storage_.Load("./assets/checkbox.bmp", "checkbox_icon");
    storage_.Load("./assets/label.bmp", "label_icon");
    storage_.Load("./assets/slidebar.bmp", "slidebar_icon");
    storage_.Load("./assets/inputbox.bmp", "inputbox_icon");
    storage_.Load("./assets/treelist.bmp", "treelist_icon");
    storage_.Load("./assets/treelist_root.png", "treelist_root_icon");
}
